//
//  mainViewController.swift
//  weibo
//
//  Created by David on 15/11/13.
//  Copyright © 2015年 DV. All rights reserved.
//

import UIKit

class mainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        addChildViewControllers()
        // Do any additional setup after loading the view.
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setupComposeBtn()
    }
    func setupComposeBtn(){
        let count = childViewControllers.count
        //计算每个tabbaritem的宽度
        let with = tabBar.bounds.width / CGFloat(count)
        //偏移2个单位
        let rect = CGRect(x: 0, y: 0, width: with, height: tabBar.bounds.height)
        composeBTN.frame = CGRectOffset(rect, with*2, 0)
        
    }
    func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", image: "tabbar_home", selectedImage: "tabbar_home_highlighted")
        addChildViewController(MessageTableViewController(), title: "消息", image: "tabbar_message_center", selectedImage: "tabbar_message_center_highlighted")
        //占个坑位
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", image: "tabbar_discover", selectedImage: "tabbar_discover_highlighted")
        addChildViewController(ProfileTableViewController(), title: "个人", image: "tabbar_profile", selectedImage: "tabbar_profile_highlighted")
    }
    func addChildViewController(vc2:UIViewController,title:String,image:String,selectedImage:String) {
        let vc = vc2
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: image)
        vc.tabBarItem.selectedImage = UIImage(named: selectedImage)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
       
    }
    //创建按钮
    private lazy var composeBTN : UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "composeClick", forControlEvents:  UIControlEvents.TouchUpInside)
        self.tabBar.addSubview(btn)
        return btn
    }()
    @objc private func composeClick(){
        print(__FUNCTION__)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
